package com.zmn.oms.api.controller.third.shunfeng;

import com.alibaba.fastjson.JSON;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.third.shunfeng.ShunfengConsts;
import com.zmn.oms.third.shunfeng.ShunfengUtil;
import com.zmn.oms.third.shunfeng.api.SFResultData;
import com.zmn.oms.third.shunfeng.dto.SFRequestDTO;
import com.zmn.oms.third.shunfeng.dto.SFResponseDTO;
import com.zmn.oms.third.shunfeng.service.ShunfengService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;
import java.util.Optional;

/**
 * 类描述：顺丰安装渠道对接接口，用于接受渠道创建和取消订单
 *
 * @author xiewenbing
 * @date 2020/12/11 11:00
 */

@RestController
@RequestMapping("/shunfeng")
@Slf4j
public class ShunfengController {

    @Autowired
    private ShunfengService shunfengService;

    @PostMapping("/api/pushOrder")
    public SFResponseDTO pushOrder(HttpServletRequest httpServletRequest, SFRequestDTO requestDTO) {

        SFResponseDTO responseDTO = new SFResponseDTO();
        if (!this.verifyRequestParam(requestDTO, responseDTO)) {

            log.info("【{}】#pushOrder#请求参数验证失败，【{}】【{}】", ShunfengConsts.CHANNEL_NAME,
                    JSON.toJSONString(requestDTO), JSON.toJSONString(responseDTO));
            return responseDTO;
        }

        SFResultData resultData = new SFResultData();

        try {
            if (Objects.equals(requestDTO.getServiceCode(),ShunfengConsts.SERVICE_CODE_PUSH_ORDER)) {
                shunfengService.handleOrderTaskRequest(requestDTO);
            } else  if (Objects.equals(requestDTO.getServiceCode(),ShunfengConsts.SERVICE_CODE_DELIVERY_NOTICE)){
                shunfengService.handleArrivalNoticeRequest(requestDTO);
            }

            resultData.setErrorMessage("success");
            resultData.setSuccess(Boolean.TRUE);
        } catch (OmsBaseException exception) {
            resultData.setErrorMessage(Optional.ofNullable(exception.getMessage()).orElse(""));
            resultData.setSuccess(Boolean.FALSE);
        }

        responseDTO.setApiResultData(JSON.toJSONString(resultData));

        log.info("【{}】#pushOrder# 处理完成 ，【{}】【{}】",ShunfengConsts.CHANNEL_NAME, JSON.toJSONString(requestDTO),
                JSON.toJSONString(responseDTO));

        return responseDTO;
    }

    /**
     * 验证请求的参数
     *
     * @param requestDTO
     * @param responseDTO
     * @return
     */
    private boolean verifyRequestParam(SFRequestDTO requestDTO, SFResponseDTO responseDTO) {

        do {
            // 参数判断
            if (Objects.isNull(responseDTO)) {
                break;
            }

            if (Objects.isNull(requestDTO) || StringUtils.isBlank(requestDTO.getMsgData())) {
                responseDTO.setApiResultCode(ShunfengConsts.RESULT_CODE_PARAM_ERROR);
                responseDTO.setApiErrorMsg("参数为空");
                break;
            }

            responseDTO.setApiResponseID(requestDTO.getRequestID());

            if (!ShunfengConsts.POWER_ON) {
                responseDTO.setApiResultCode(ShunfengConsts.RESULT_CODE_INVALID_PRIVILEGE);
                responseDTO.setApiErrorMsg("接口暂不支持");
                break;
            }

            if (!ShunfengUtil.verifySign(requestDTO)) {
                responseDTO.setApiResultCode(ShunfengConsts.RESULT_CODE_INVALID_SIGN);
                responseDTO.setApiErrorMsg("签名校验失败");
                break;
            }

            if (!Objects.equals(requestDTO.getServiceCode(),ShunfengConsts.SERVICE_CODE_PUSH_ORDER) &&
                    !Objects.equals(requestDTO.getServiceCode(),ShunfengConsts.SERVICE_CODE_DELIVERY_NOTICE)) {
                responseDTO.setApiResultCode(ShunfengConsts.RESULT_CODE_SERVICE_ERROR);
                responseDTO.setApiErrorMsg("未知的服务代码");
                break;
            }

            responseDTO.setApiResultCode(ShunfengConsts.RESULT_CODE_SUCCESS);

            return true;
        } while (false);

        return false;
    }
}
